//
//  ShezhiViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/4/8.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit
import MJRefresh
import RxSwift
import RxDataSources
import Then
import SDCycleScrollView

class ShezhiViewController: SYBaseTableViewController {
    
    fileprivate var vm: VGTableViewPageViewModel<CGQyuweiModel>!
   
    var delete: UIButton = {
        let v = UIButton()
        v.setTitle("退出", for: .normal)
        v.setTitleColor(UIColor(hex: 0x0782FF), for: .normal)
        v.titleLabel?.font = 18.autoFontSize()
        v.layer.cornerRadius = 3
        v.layer.masksToBounds = true
        v.backgroundColor = UIColor.white
        return v
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTableView()
        setNoDataView()

        self.loadDataSuccessed()
        
        self.view.backgroundColor = Constant.bgViewColor
        self.title = "设置"
    }
    
    
    
    @objc func rightAction() {
        
    }
    
    func loadDataSuccessed() {
        DispatchQueue.main.async {
            
            self.setDataSource()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = .clear
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.view.addSubview(self.tableView!)
        
        tableView.rx.itemSelected
            .subscribe(onNext:{ [weak self] indexPath in
                if let `self` = self {
                    
                }
            })
            .disposed(by:dispose)
        
        delete.frame = CGRect(x: 10.autoWidth(), y: SCREEN_HEIGHT - 64.autoWidth() - BOTTOM_SAFE_HEIGHT, width: SCREEN_WIDTH - 20.autoWidth(), height: 44.autoWidth())
     
        self.view.addSubview(delete)
        
        delete.addAction {[weak self] in
            if let `self` = self {
                
                self.clearAllUserDefaultsData()
                SYTokenCheck.toLoginVC()

            }
        }
    }
    
    
    /** 清除userDefaults所有数据*/
     
    func clearAllUserDefaultsData(){
       let userDefaults = UserDefaults.standard
       let dics = userDefaults.dictionaryRepresentation()
       for key in dics {
           userDefaults.removeObject(forKey: key.key)
       }
       userDefaults.synchronize()
    }
    func setNoDataView() {
        noDataView = SYNoDataView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT))
        noDataView.isHidden = true
        tableView.addSubview(noDataView)
    }
    
    func setDataSource() {
        //初始化数据
        let sections = Observable.just([
            CGQyuweiModelSection(header: "", items: [CGQyuweiModel(),CGQyuweiModel(),CGQyuweiModel(),CGQyuweiModel(), CGQyuweiModel()])//self.vm.dataSource)
        ])
        
        //创建数据源
        let dataSource = RxTableViewSectionedAnimatedDataSource<CGQyuweiModelSection>(
            //设置单元格
            configureCell: { ds, tv, ip, item in
                let cell: CGQshezhiCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                    as? CGQshezhiCell ?? CGQshezhiCell(style: .default, reuseIdentifier: "Cell")
                switch ip.row {
                case 0:
                    cell.imgView.sy_name("set_zhanghao")
                    cell.tipText.text = "账号与安全"
                case 1:
                    cell.imgView.sy_name("set_yinsi")
                    cell.tipText.text = "隐私"
                case 2:
                    cell.imgView.sy_name("set_gongju")
                    cell.tipText.text = "通用"
                    cell.showSep()
                case 3:
                    cell.imgView.sy_name("set_fankui")
                    cell.tipText.text = "帮助与反馈"
                case 4:
                    cell.imgView.sy_name("set_about")
                    cell.tipText.text = "关于我们"
                default:break
                }
//                cell.configureView(model: item)
                cell.selectionStyle = .none
                return cell
                
            },
            //设置分区头标题
            titleForHeaderInSection: { ds, index in
                return ds.sectionModels[index].header
            })
        tableView.dataSource = nil
        //绑定单元格数据
        sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: dispose)
        
    }
    
    
}


//自定义Section
//struct CGQyuweiModelSection {
//    var header: String
//    var items: [Item]
//}
//
//extension CGQyuweiModelSection : AnimatableSectionModelType {
//    typealias Item = CGQyuweiModel
//
//    var identity: String {
//        return header
//    }
//
//    init(original: CGQyuweiModelSection, items: [Item]) {
//        self = original
//        self.items = items
//    }
//}
//
//extension CGQyuweiModel: IdentifiableType {
//    var identity: String {
//        return id!
//    }
//
//    typealias Identity = String
//}


class CGQshezhiCell: SYBaseCell {
    
    let imgView: UIImageView = {
        let v = UIImageView()
        
        return v
    }()
    
    let tipText: UILabel = {
        let v = UILabel()
        
        v.font = 15.autoFontSize()
        v.textColor = UIColor(hex: 0x333333)
        return v
    }()
    
    let arrow: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "my_arrow")
        return v
    }()
    
    var sepView: UIView = {
        let v = UIView()
        v.backgroundColor = Constant.bgViewColor
        v.isHidden = true
        return v
    }()
    
    func showSep() {
        sepView.isHidden = false
        imgView.snp.remakeConstraints { (make) in
            make.top.equalToSuperview().offset(16.autoWidth())
            make.left.equalToSuperview().offset(10.autoWidth())
            make.width.equalTo(16.autoWidth())
            make.height.equalTo(16.autoWidth())
            make.bottom.equalToSuperview().offset(-26.autoWidth())
        }
    }
    
    override func initViews() {
        
        addSubview(sepView)
        addSubview(imgView)
        addSubview(tipText)
        addSubview(arrow)
        addSubview(lineView)
        imgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16.autoWidth())
            make.left.equalToSuperview().offset(10.autoWidth())
            make.width.equalTo(16.autoWidth())
            make.height.equalTo(16.autoWidth())
            make.bottom.equalToSuperview().offset(-16.autoWidth())
        }
        tipText.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16.autoWidth())
            make.left.equalToSuperview().offset(33.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        arrow.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16.autoWidth())
            make.right.equalToSuperview().offset(-10.autoWidth())
            make.width.equalTo(16.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        lineView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(47.5.autoWidth())
            make.left.equalToSuperview().offset(10.autoWidth())
            make.right.equalToSuperview().offset(-10.autoWidth())
            make.height.equalTo(0.5.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(10.autoWidth())
        }
    }
}
